package com.zzx.day57Demo.action;

import java.io.File;
import java.util.List;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.zzx.day57Demo.Biz.CraftBiz;
import com.zzx.day57Demo.model.Craft;

@Controller
@Scope("prototype")
@Namespace(value = "/Craft")
@Results({@Result(name = "list", location = "list.jsp"),
	@Result(name = "input", location = "add.jsp")})
public class CraftAction extends BaseAction{
	
	private CraftBiz craftBiz;
	private List<Craft>craList;
	private Craft cra;
	private File photoFile;
	private String savePath= "D:/DecorateImg/";
	private String photoFileContentType;
	private String photoFileFileName;
	
	@Action("list")
	public String list(){
		craList= craftBiz.findAll();
		
		return "list";
	}
	@Action("add")
	public String add(){
		return "input";
	}
	@Action("saveOrUpdate")
	public String saveOrUpdate(){
		String url = UUID.randomUUID().toString();
		cra.setCraImg(url);
		cra.setCraOvertime(photoFileFileName);
		craftBiz.saveOrUpdate(cra);
		System.out.println(cra.getCraImg()+"111111111111111111111111");
		File f = new File(savePath + url);
		try {
			FileUtils.copyFile(photoFile, f);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list();
		
	}
	@Action("delete")
	public String delete(){
		
		craftBiz.delete(cra.getCraId());
		return list();
	}
	@Action("update")
	public String update(){
		cra = craftBiz.findById(cra.getCraId());
		return add();
	}
	
	
	
	
	
	
	public List<Craft> getCraList() {
		return craList;
	}
	public void setCraList(List<Craft> craList) {
		this.craList = craList;
	}
	public Craft getCra() {
		return cra;
	}
	public void setCra(Craft cra) {
		this.cra = cra;
	}
	public void setCraftBiz(CraftBiz craftBiz) {
		this.craftBiz = craftBiz;
	}
	public File getPhotoFile() {
		return photoFile;
	}
	public void setPhotoFile(File photoFile) {
		this.photoFile = photoFile;
	}
	public String getSavePath() {
		return savePath;
	}
	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}
	public String getPhotoFileContentType() {
		return photoFileContentType;
	}
	public void setPhotoFileContentType(String photoFileContentType) {
		this.photoFileContentType = photoFileContentType;
	}
	public String getPhotoFileFileName() {
		return photoFileFileName;
	}
	public void setPhotoFileFileName(String photoFileFileName) {
		this.photoFileFileName = photoFileFileName;
	}
	


}
